package com.jhhc.Http.ServiceImpl;

import com.jhhc.Http.Pojo.RequestPojo;
import com.jhhc.Http.Service.DeleteHxDataService;
import com.jhhc.Http.Service.GetUrlService;
import com.jhhc.Http.Service.InsertHxDataService;
import com.jhhc.RHDB.Data.Pojo.ST_PPTN_R;
import com.jhhc.RHDB.Data.Pojo.ST_RIVER_R;
import com.jhhc.RHDB.Data.Pojo.ST_RSVR_R;
import dm.jdbc.filter.stat.json.JSONArray;
import dm.jdbc.filter.stat.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GetUrlServiceImpl {
    @Autowired
    private InsertHxDataService insertHxDataService;
    @Autowired
    private DeleteHxDataService deleteHxDataService;


    public static String getData(String url,String token,RequestPojo requestPojo) throws Exception{

        //根据获取到的url 和 token 去请求获取数据的链接
        CloseableHttpClient httpClient = HttpClients.createDefault();  //http请求
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/json");
        httpPost.addHeader("token",token);
        JSONObject J = new JSONObject(requestPojo);
        StringEntity entity2 = new StringEntity(String.valueOf(J), ContentType.APPLICATION_JSON);
        httpPost.setEntity(entity2);
        CloseableHttpResponse response = httpClient.execute(httpPost);
        System.out.println(response.getStatusLine().getStatusCode() + "\n");
        HttpEntity entity = response.getEntity();
        String responseContent = EntityUtils.toString(entity, "UTF-8");
        response.close();
        httpClient.close();
        //获取到数据后入库
        return responseContent;

    }


    //根据不同的token请求链接获取token和url
    //降雨量
    public boolean getPPTNdata() throws Exception{
        String Url ="https://sgpt.zjwater.com/service/v2/auth/generateToken/d4d32cf3";
        JSONObject message =  GetUrlService.getTokenUrl(Url);
        String url = String.valueOf(message.getMap().get("url"));
        String token = String.valueOf(message.getMap().get("token"));
        //创建请求参数
        RequestPojo requestPojo = new RequestPojo();
        //requestPojo.setStcds("2592,3515,2587,2586,6570,3518,5919,5925,5918");
        requestPojo.setStcds("6568");
        requestPojo.setStartTime("2022-05-31 08:00:00");
        requestPojo.setEndTime("2022-06-01 08:00:00");
        requestPojo.setPageNumber(1);
        requestPojo.setPageSize(1);
        requestPojo.setGroup(1);
        //根据获取的url和token去请求数据
        JSONObject json =  new JSONObject(getData(url,token,requestPojo));
        //根据,分割出站码
        String[] s = requestPojo.getStcds().split(",");
        List<String> stcds = new ArrayList<>(Arrays.asList(s));
        //System.out.println(stcds);
        //获取的结果
        //System.out.println(json);
        //获取message层json
        JSONObject js =  new JSONObject(json.getMap().get("message"));
        //System.out.println("js"+js);
        //获取map
        JSONObject js1 =  js.getJSONObject("map");
        System.out.println(js1);
//        JSONArray jsonarr= null;
//        //创建pptn集合
//        List<ST_PPTN_R> rList = new ArrayList<>();
//        //获取到jsonarr后赋值数据
//        for (String stcd:stcds){
//            //根据站码分成json数组
//            jsonarr = js1.getJSONArray(stcd);
//            //循环数组
//            for(int i=0;i<jsonarr.length();i++){
//                //转成json
//                JSONObject jsonObject = (JSONObject)jsonarr.get(i);
//                ST_PPTN_R st_pptn_r = new ST_PPTN_R();
//                //根据key获取数据
//                st_pptn_r.setStcd(stcd);
//                st_pptn_r.setTm(jsonObject.getString("tm"));
//                st_pptn_r.setDrp(jsonObject.getDouble("drp"));
//                rList.add(st_pptn_r);
//            }
//        }
//        //先删后入
//        //调用删除
//        int re = deleteHxDataService.DeleteHxPPTNData(stcds,requestPojo.getStartTime(),requestPojo.getEndTime());
//        System.out.println("雨量删除方法返回值:"+re);
//        //调用入库方法
//        int res = insertHxDataService.insertHxPPTNData(rList);
//        System.out.println("雨量插入方法返回值:"+res);
//        System.out.println("插入数据条数:"+rList.size());
//        return res>0;
        return false;
    }





    //水库水情
    public boolean getRSVRdata() throws Exception{
        //token请求地址
        String Url ="https://sgpt.zjwater.com/service/v2/auth/generateToken/e90ddbcc";
        //调用请求token方法
        JSONObject message =  GetUrlService.getTokenUrl(Url);
        String url = String.valueOf(message.getMap().get("url"));
        String token = String.valueOf(message.getMap().get("token"));
        //创建请求对象
        RequestPojo requestPojo = new RequestPojo();
        requestPojo.setStcds("6568");
        requestPojo.setStartTime("2022-05-31 08:00:00");
        requestPojo.setEndTime("2022-06-01 08:00:00");
        requestPojo.setSttp("RR");
        requestPojo.setPageNumber(1);
        requestPojo.setPageSize(1);
        requestPojo.setGroup(1);
        //根据获取的url和token去请求数据
        JSONObject json =  new JSONObject(getData(url,token,requestPojo));
        String[] s = requestPojo.getStcds().split(",");
        List<String> stcds = new ArrayList<>(Arrays.asList(s));
        //System.out.println(stcds);
        //获取的结果
        //System.out.println(json);
        //获取message
        JSONObject js =  new JSONObject(json.getMap().get("message"));
        //System.out.println("js"+js);
        //获取map
        JSONObject js1 =  js.getJSONObject("map");
        System.out.println(js1);
//        //声明json数组
//        JSONArray jsonarr= null;
//        //创建水库集合
//        List<ST_RSVR_R> rList = new ArrayList<>();
//        //获取到jsonarr后赋值数据
//        for (String stcd:stcds){
//            jsonarr = js1.getJSONArray(stcd);
//            for(int i=0;i<jsonarr.length();i++){
//                JSONObject jsonObject = (JSONObject)jsonarr.get(i);
//                ST_RSVR_R st_rsvr_r = new ST_RSVR_R();
//                st_rsvr_r.setStcd(stcd);
//                st_rsvr_r.setTm(jsonObject.getString("tm"));
//                st_rsvr_r.setRz(jsonObject.getDouble("z"));
//                rList.add(st_rsvr_r);
//            }
//        }
//        //先删后入
//        //调用删除
//        int re = deleteHxDataService.DeleteHxRSVRData(stcds,requestPojo.getStartTime(),requestPojo.getEndTime());
//        System.out.println("水库删除方法返回值:"+re);
//        //rList 数据集合  调用入库方法
//        int res = insertHxDataService.insertHxReservoirData(rList);
//        System.out.println("水库插入方法返回值:"+res);
//        System.out.println("插入数据条数:"+rList.size());
//        return res>0;
        return false;
    }




    //河道水情
    public String getRIVERdata(RequestPojo requestPojo) throws Exception{
        String Url ="https://sgpt.zjwater.com/service/v2/auth/generateToken/e7e86b79";
        JSONObject message =  GetUrlService.getTokenUrl(Url);
        String url = String.valueOf(message.getMap().get("url"));
        String token = String.valueOf(message.getMap().get("token"));
        //根据获取的url和token去请求数据
        JSONObject json =  new JSONObject(getData(url,token,requestPojo));
        //根据,分割出站码
        String[] s = requestPojo.getStcds().split(",");
        List<String> stcds = new ArrayList<>(Arrays.asList(s));
        System.out.println(stcds);
        //获取的结果
        System.out.println(json);
        //获取message层json
        JSONObject js =  new JSONObject(json.getMap().get("message"));
        System.out.println("js"+js);
        //获取map
        JSONObject js1 =  js.getJSONObject("map");
        //声明json数组
        JSONArray jsonarr= null;
        //创建河道表集合
        List<ST_RIVER_R> rList = new ArrayList<>();
        //获取到jsonarr后赋值数据
        for (String stcd:stcds){
            //根据站码分成json数组
            jsonarr = js1.getJSONArray(stcd);
            //循环数组
            for(int i=0;i<jsonarr.length();i++){
                //转成json
                JSONObject jsonObject = (JSONObject)jsonarr.get(i);
                ST_RIVER_R st_river_r = new ST_RIVER_R();
                //根据key获取数据
                st_river_r.setStcd(stcd);
                st_river_r.setTm(jsonObject.getString("tm"));
                st_river_r.setZ(jsonObject.getDouble("z"));
                st_river_r.setZ(jsonObject.getDouble("q"));
                System.out.println(st_river_r.toString());
                rList.add(st_river_r);
            }
        }
        System.out.println(rList.size());
        return getData(url,token,requestPojo);
    }

















//    public static String sendHttpPost(RequestPojo requestPojo) throws Exception {
//        CloseableHttpClient httpClient = HttpClients.createDefault();  //http请求
//
//        HttpPost tokenHttpPost = new HttpPost("https://sgpt.zjwater.com/service/v2/auth/generateToken/e90ddbcc");
//
//        JSONObject j  = GetUrlService.getRhdbToken();
//
//        StringEntity entity1 = new StringEntity(String.valueOf(j), ContentType.APPLICATION_JSON);
//
//        tokenHttpPost.setEntity(entity1);
//
//        CloseableHttpResponse tokenResponse = httpClient.execute(tokenHttpPost);
//        System.out.println(tokenResponse.getStatusLine().getStatusCode() + "\n");
//        HttpEntity tokenEntity = tokenResponse.getEntity();
//        String tokenResponseContent = EntityUtils.toString(tokenEntity, "UTF-8"); //返回json
//
//
//        System.out.println("loginResultJson:" + tokenResponseContent);
//
//        JSONObject  json = new JSONObject(tokenResponseContent); //string转json
//        String message = String.valueOf(json.getMap().get("message"));
//        JSONObject  json1 = new JSONObject(message);
//
//
//        //第二次请求
//
//
//        //添加请求参数为 开始，结束时间，页面数，页面显示数量
//        //添加后把对象转换成json传给url
//        //获取后得到string
//
//        String Url = String.valueOf(json1.getMap().get("url"));
//        String Token =String.valueOf(json1.getMap().get("token"));
//
//        HttpPost httpPost = new HttpPost(Url);
//        httpPost.addHeader("Content-Type", "application/json");
//        httpPost.addHeader("token",Token);
//
//        JSONObject J = new JSONObject(requestPojo);
//        StringEntity entity2 = new StringEntity(String.valueOf(J), ContentType.APPLICATION_JSON);
//        httpPost.setEntity(entity2);
//        CloseableHttpResponse response = httpClient.execute(httpPost);
//        System.out.println(response.getStatusLine().getStatusCode() + "\n");
//        HttpEntity entity = response.getEntity();
//        String responseContent = EntityUtils.toString(entity, "UTF-8");
//        System.out.println("loginResultJson:" + responseContent);
//
//
//        System.out.println(responseContent);
//
//        response.close();
//        httpClient.close();
//        return responseContent;
//    }



}
